package com.zhuoyue.biz.core.command.impl.curtains;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.CmdParams;
import com.zhuoyue.biz.core.command.model.SilentCurtainCmd;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Title: 智能窗帘
 * <p>Description:
 * <p>Company: JiZi
 *
 * @author wjming
 * @version 1.0
 * @date 2018年12月10日
 */
public class IntelligentCurtain0Fe60210{
    /**
     * 数据帧模型
     */
    public static final String CMD_MODEL = "0701${gatewayMac}${deviceMac}06${status}";
    /**
     * 不同状态对应的OD类型
     */
    public static final String OD_OPEN = "FF00003D434C";
    public static final String OD_CLOSE = "FF00003D4F50";
    public static final String OD_STOP = "FF00003D5354";
    public static final String OD_0 = "FF00003D1025";
    public static final String OD_25 = "FF00003D0C25";
    public static final String OD_50 = "FF00003D0825";
    public static final String OD_75 = "FF00003D0425";
    public static final String OD_100 = "FF00003D0025";
    public static final String OD_CHECK = "FF00003D504F";

    /**
     * 请求的参数状态
     */
    public static final String OPEN = "OPEN";
    public static final String CLOSE = "CLOSE";
    public static final String STOP = "STOP";
    public static final String PERCENT_0 = "0";
    public static final String PERCENT_25 = "25";
    public static final String PERCENT_50 = "50";
    public static final String PERCENT_75 = "75";
    public static final String PERCENT_100 = "100";

    public static String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";
        if (CmdParams.ControlParam.SWITCH_STOP.equals(controlType)) {
            cmd = getSwitchStopCmd(map);
        } else if (CmdParams.ControlParam.PERCENT.equals(controlType)) {
            cmd = getPercentCmd(map);
        } else if (CmdParams.ControlParam.CHECK.equals(controlType)) {
            cmd = getCheckCmd(map);
        }

        return cmd;
    }

    private static String getCheckCmd(Map<String, String> map) {
        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        return new SilentCurtainCmd(gatewayMac, deviceMac, OD_CHECK).getCmd(CMD_MODEL);

    }


    private static String getSwitchStopCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String state = actionsMap.get("state");

        if (OPEN.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_OPEN).getCmd(CMD_MODEL);
        } else if (CLOSE.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_CLOSE).getCmd(CMD_MODEL);
        } else if (STOP.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_STOP).getCmd(CMD_MODEL);
        }
        return null;
    }

    /**
     * 百分比
     *
     * @param map
     * @return
     */
    private static String getPercentCmd(Map<String, String> map) {
        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String value = actionsMap.get("value");
        if (PERCENT_0.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_0).getCmd(CMD_MODEL);
        } else if (PERCENT_25.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_25).getCmd(CMD_MODEL);
        } else if (PERCENT_50.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_50).getCmd(CMD_MODEL);
        } else if (PERCENT_75.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_75).getCmd(CMD_MODEL);
        } else if (PERCENT_100.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OD_100).getCmd(CMD_MODEL);
        }
        return null;
    }

}
